import json
from datetime import datetime

from flask import Flask, Blueprint, Response, request
from flask_jwt_extended import jwt_required
from app.common.commonMessage import CommonMassages

from app.config.db import DBUtils
from app.config.jsonEncoder import CustomJSONEncoder
from app.service.businessRiskService import businessRiskService

app = Flask(__name__)
# 创建蓝图
business_risk_up = Blueprint('business_risk_controller', __name__, url_prefix='/kshx')
# 调用服务
BusinessRiskService = businessRiskService()
# 获取session
db_utils = DBUtils()
session = db_utils.get_session()

@business_risk_up.route('/business-risk', methods=['GET'])
@jwt_required()
def get_business_risk():
    pageNum = int(request.args.get('pageNum'))
    pageSize = int(request.args.get('pageSize'))
    risk_type = request.args.get('riskType')
    data = BusinessRiskService.select_businessrisk_by_type(session,pageNum,pageSize,risk_type)
    if data is not None:
        respResult = {
            "code": 200,
            "data": data,
            "message": CommonMassages.CCD_SELECT_SUCCESS.value
        }
    else:
        respResult = {
            "code": 400,
            "data": None,
            "message": CommonMassages.CCD_SELECT_FAILED.value
        }
    resp_data = json.dumps(respResult, ensure_ascii=False, default=str)
    return Response(resp_data, content_type='application/json')

@business_risk_up.route('/judicial-risk', methods=['GET'])
@jwt_required()
def get_judicial_risk():
    pageNum = int(request.args.get('pageNum'))
    pageSize = int(request.args.get('pageSize'))
    risk_type = request.args.get('riskType')
    data = BusinessRiskService.select_judicialrisk_by_type(session,pageNum,pageSize,risk_type)
    if data is not None:
        respResult = {
            "code": 200,
            "data": data,
            "message": CommonMassages.CCD_SELECT_SUCCESS.value
        }
    else:
        respResult = {
            "code": 400,
            "data": None,
            "message": CommonMassages.CCD_SELECT_FAILED.value
        }
    resp_data = json.dumps(respResult, ensure_ascii=False, default=str)
    return Response(resp_data, content_type='application/json')

@business_risk_up.route('/risk-search', methods=['GET'])
@jwt_required()
def get_risk_search():
    pageNum = int(request.args.get('pageNum'))
    pageSize = int(request.args.get('pageSize'))
    risk_type = request.args.get('riskType')
    company_name = request.args.get('companyName')
    data = BusinessRiskService.select_risk_by_company(session,pageNum,pageSize,risk_type,company_name)
    if data is not None:
        respResult = {
            "code": 200,
            "data": data,
            "message": CommonMassages.CCD_SELECT_SUCCESS.value
        }
    else:
        respResult = {
            "code": 400,
            "data": None,
            "message": CommonMassages.CCD_SELECT_FAILED.value
        }
    resp_data = json.dumps(respResult, ensure_ascii=False, default=str)
    return Response(resp_data, content_type='application/json')